package com.airnews.ticket.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.airnews.ticket.R;
import com.airnews.ticket.model.City;
import com.airnews.ticket.util.Constants;

/**
 * 城市列表
 * 
 */
public class CityList extends BaseActivity /*implements OnClickListener*/ {

	private ListView list_view;
	private static int cityPos = -1;//城市位置

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.city_select_layout);
		super.onCreate(savedInstanceState);

		setTitleBar(R.string.title_city_select);
		
		setupView();
		
		cityPos = getIntent().getIntExtra("city_pos", -1);

	}

	private void setupView() {
		list_view = (ListView) findViewById(R.id.list_view);
		list_view.setAdapter(new CityListAdapter(getLayoutInflater()));
		list_view.setOnItemClickListener(onItemClickListener);
	}

	private OnItemClickListener onItemClickListener = new OnItemClickListener(){

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			//cityPos = position;
			Intent it = new Intent();
			it.putExtra("city_pos", position);
			setResult(RESULT_OK, it);
			finish();
		}
		
	};
	

	private static class CityListAdapter extends BaseAdapter {
		
		private LayoutInflater layoutInflater;

		public CityListAdapter(LayoutInflater layoutInflater) {
			this.layoutInflater = layoutInflater;
		}

		@Override
		public int getCount() {
			return Constants.CITY_LIST.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if(convertView == null){
				viewHolder = new ViewHolder();
				convertView = layoutInflater.inflate(R.layout.city_select_item, null);
				viewHolder.item_title = (TextView)convertView.findViewById(R.id.item_title);
				viewHolder.selected_item = (ImageView)convertView.findViewById(R.id.selected_item);
				viewHolder.city_info = (TextView)convertView.findViewById(R.id.city_info);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder)convertView.getTag();
			}
			City city = Constants.CITY_LIST.get(position);
			if(cityPos == position){
				viewHolder.selected_item.setVisibility(View.VISIBLE);
			}else{
				viewHolder.selected_item.setVisibility(View.INVISIBLE);
			}
			//viewHolder.selected_item.setImageResource(resId);
			viewHolder.city_info.setText(city.cityName);
			if(!TextUtils.isEmpty(city.cityType)){
				viewHolder.item_title.setText(city.cityType);
				viewHolder.item_title.setVisibility(View.VISIBLE);
			}else{
				viewHolder.item_title.setVisibility(View.GONE);
			}
			return convertView;
		}
		
		static class ViewHolder{
			TextView item_title;
			ImageView selected_item;
			TextView city_info;
		}
		
	}

	

}